/**
 * 
 */
package org.rhq.plugins.tools.httpcheck;

import java.util.HashSet;
import java.util.Set;

import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;

/**
 * @author lgao
 *
 */
public class HttpPathDiscoveryComponent implements ResourceDiscoveryComponent<HttpPathResourceComponent>, HttpCheckerConstants{

	public Set<DiscoveredResourceDetails> discoverResources(
			ResourceDiscoveryContext<HttpPathResourceComponent> context)
			throws InvalidPluginConfigurationException, Exception {
		Set<DiscoveredResourceDetails> result = new HashSet<DiscoveredResourceDetails>();
		Configuration parentConfig = context.getParentResourceContext().getPluginConfiguration();
		String host = parentConfig.getSimpleValue(PLUGIN_CONFIG_HOST_KEY, null);
		String port = parentConfig.getSimpleValue(PLUGIN_CONFIG_PORT_KEY, "80");
		for(Configuration config: context.getPluginConfigurations()){
			String path = config.getSimpleValue(PLUGIN_CONFIG_PATH_KEY, null);
			String resourceKey = "HTTP_CHECK_" + host + ":" + port + "" + path;
			String resourceName = "Http Checker on: " + host + ":" + port + "" + path;
			String resourceVersion = "1.0.0";
			String resourceDescription = "Http Checker on server: " + path;
			DiscoveredResourceDetails resource = new DiscoveredResourceDetails(context.getResourceType(), resourceKey, resourceName, resourceVersion, resourceDescription, config, null);
			result.add(resource);
		}
		return result;
	}

}
